<?php
namespace app\api\service;
use think\Service;
use app\admin\model\user\Balance;
use app\admin\model\user\Recharge;
use app\admin\model\Withdrawal;
class userService
{
    static function withdrawal($uid = 0,$money = 0,$admin_id = 0)
    {   
        if($uid && $money && $admin_id){
            // 获取今天的开始和结束时间戳
            $startOfDay = strtotime('today');
            $endOfDay = strtotime('tomorrow') - 1;

            $find = Withdrawal::where('timestamp', 'between', [$startOfDay, $endOfDay])->find();
            if($find){
                  return ['code'=>100,'msg'=>'今日已提交审核'];
            }
            $r =  Withdrawal::create([
                'uid'=>$uid,
                'money'=>$money,
                'admin_id'=>$admin_id
            ]);
            if($r){
                 return ['code'=>200,'msg'=>'ok'];
            }else{
                 return ['code'=>100,'msg'=>'申请失败，请稍后重试'];
            }
        } 
        return ['code'=>100,'msg'=>'error!'];
    }

    static function withdrawalRecord($page,$uid){
            $data =  Withdrawal::where('admin_id', $uid)->order("id desc")->page($page, 10)->select();
            $total = Withdrawal::where('admin_id', $uid)->count();
            return ['data'=>$data,'total'=>$total];
            
    }

    static function rechargeRecord($uid){
            return Recharge::where("uid",$uid)->order("id desc")->select();
    }
    static function rechargeOrder($uid = 0,$params = []){
        try {
            $ordernum  = time().rand(1000,9999);
            $result = Recharge::create([
                    'uid'=>$uid,
                    'money'=>$params['money'] ?? 0,
                    'ordernum'=>$ordernum

            ]);
            return ['code' => 200, 'ordernum' => $ordernum];
        } catch (\Exception $e) {
            return ['code' => 400, 'msg' => $e->getMessage()];
        }
    }
//
    static function balanceRecord($params = []){
        try { 
            $result = Balance::create([
                    'uid'=>$params['uid'],
                    'money'=>$params['money'] ?? 0,
                    'note'=>$params['note'] ?? '',
                    'type'=>$params['type']

            ]);
            return ['code' => $result ? 200 : 100];
        } catch (\Exception $e) {
            return ['code' => 400, 'msg' => $e->getMessage()];
        }
    }

}